package com.songlanyun.modules.integralrecord.controller.admin;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.modules.account.entity.UserIdentityAssetEntity;
import com.songlanyun.modules.account.service.UserIdentityAssetService;
import com.songlanyun.modules.applyareaagent.entity.ApplyAreaAgentEntity;
import com.songlanyun.modules.applyareaagent.service.ApplyAreaAgentService;
import com.songlanyun.modules.pointconfig.entity.PointConfig;
import com.songlanyun.modules.pointconfig.service.PointConfigService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.integralrecord.entity.IntegralRecord;
import com.songlanyun.modules.integralrecord.service.IntegralRecordService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;


/**
 * 会员积分的收支记录
 *
 * @author wuchao
 * @email sunlightcs@gmail.com
 * @date 2021-02-25 15:52:20
 */
@RestController
@RequestMapping("admin/integral/record")
public class IntegralRecordAdminController extends AbstractController {
    @Autowired
    private IntegralRecordService integralRecordService;
    @Autowired
    private ApplyAreaAgentService applyAreaAgentService;

    @Autowired
    private UserIdentityAssetService userIdentityAssetService;

    @Autowired
    private PointConfigService pointConfigService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("integralrecord:integralrecord:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = integralRecordService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 区域代理的的积分日志
     */
    @GetMapping("/myPointPage")
    //@RequiresPermissions("integralrecord:integralrecord:list")
    public R myPointPage(@RequestParam Map<String, Object> params) {
        Long areaAgentId = getAreaAgentId(true);
        ApplyAreaAgentEntity entity = applyAreaAgentService.loadById(areaAgentId, true);
        params.put("userId",entity.getUserId());
        PageUtils page = integralRecordService.queryPage(params);

        UserIdentityAssetEntity userIdentityAssetEntity = userIdentityAssetService.loadByIdForLock(entity.getUserId(), true);
        PointConfig config = pointConfigService.loadConfig(true);
        return R.ok().put("page", page).put("integralAble",userIdentityAssetEntity.getIntegralAble()).put("pointPrice",config.getPointPrice());
    }



    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("integralrecord:integralrecord:info")
    public R info(@PathVariable("id") Long id) {
        IntegralRecord integralRecord = integralRecordService.getById(id);

        return R.ok().put("integralRecord", integralRecord);
    }

    /**
     * 指定会员的积分记录
     */
    @GetMapping("/member/{userId}")
    //@RequiresPermissions("integralrecord:integralrecord:list")
    public R myList(@PathVariable("userId") Long userId, @RequestParam Map<String, Object> params) {
        params.put("userId", userId);
        PageUtils page = integralRecordService.queryPage(params);

        return R.ok().put("page", page);
    }


}
